package org.streets.eis.component.tabs;

import java.util.List;

import org.apache.wicket.behavior.AbstractBehavior;
import org.apache.wicket.extensions.markup.html.tabs.ITab;
import org.apache.wicket.extensions.markup.html.tabs.TabbedPanel;
import org.apache.wicket.markup.ComponentTag;
import org.apache.wicket.markup.html.IHeaderResponse;

import org.streets.eis.component.WebResources;

public class TabsPanel extends TabbedPanel{

	private static final long serialVersionUID = 1L;

	public TabsPanel(String id,List<ITab> tabs) {
		super(id,tabs);
		add(new AbstractBehavior(){
			private static final long serialVersionUID = 1L;
			@Override
			public void renderHead(IHeaderResponse response) {
				TabsPanel.this.renderReference(response);
			}
		});
	}
		
	protected void renderReference(IHeaderResponse response) {
		response.renderCSSReference(WebResources.Streets.Tabs.TAB_CSS);
	}	
	
	@Override
	public final void onComponentTag(final ComponentTag tag) {
		super.onComponentTag(tag);
		tag.put("class", "tabpanel");		
	}

	public final Tab getTabByIndex(int index){
		ITab tab = getTabs().get(index);
		if (tab instanceof Tab){
			return (Tab) tab;
		} else {
			return null;
		}
	}

	public final Tab getTabById(String id){
		for (ITab tab : getTabs()){
			if ((tab instanceof Tab) && id.equals(((Tab)tab).getId())){
				return (Tab) tab;
			}
		}
		return null;
	}
	
	public final Tab getCurrentTab(){
		return getTabByIndex(getSelectedTab());
	}	
}
